//
//  CYWebDetailCell.m
//  CreditAddressBook
//
//  Created by Lee on 15/5/11.
//  Copyright (c) 2015年 Lee. All rights reserved.
//

#import "CYWebDetailCell.h"
#import "CYDefaultRecordAO.h"

@interface CYWebDetailCell ()<UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *htmlWebView;
@end

@implementation CYWebDetailCell

- (void)setDefrs:(NSArray *)defrs {
    NSMutableString *html = [NSMutableString string];
    for (CYDefaultRecordAO *defr in defrs) {
        [html appendString:[self htmlFormat:defr.defaultContent]];
        [html appendFormat:@"<br\\><br\\><p style=\"font-size:small;\">&nbsp;&nbsp;*以上信息由  %@  提供</p>", defr.publicSource];
        [html appendString:@"<br\\>&nbsp;<br\\>"];
        
    }
    
    self.html = html;
}

- (void)setHtml:(NSString *)html {
    self.htmlWebView.delegate = self;
    
    [self.htmlWebView loadHTMLString:html baseURL:nil];
    
    //取出scrollview
    UIScrollView *tempView = [self.htmlWebView.subviews objectAtIndex:0];
    tempView.scrollEnabled = NO;
}

+ (instancetype)cellWithTableView:(UITableView *)tableView {
    static NSString *ID = @"newCell";
    CYWebDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"CYWebDetailCell" owner:nil options:nil] lastObject];
    }
    return cell;
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//    CYLog(@"webH:%f", webView.scrollView.contentSize.height);
    //若self.height
    if (!self.isLoaded) {
        if ([self.detailDeldgate respondsToSelector:@selector(cell:cellHeightChange:)]) {
            [self.detailDeldgate cell:self cellHeightChange:webView.scrollView.contentSize.height];
        }
    }
}

- (NSString *)htmlFormat:(NSString *)html {
    //截取HTML字符串设置字体样式
    NSString *a = [html substringFromIndex:6];
    NSString *htmlStr = [@"<table style='font-size:small;'" stringByAppendingString:a];
    NSString *htmlStrRep = [htmlStr stringByReplacingOccurrencesOfString:@"border=\"0\"" withString:@"border='1'"];
    //判断是否包含字符串
    NSRange range = [htmlStr rangeOfString:@"width: 200px;"];
    if (range.length > 0) {
        htmlStrRep = [htmlStrRep stringByReplacingOccurrencesOfString:@"width: 200px;" withString:@"width: 80px;"];
    }
    return htmlStrRep;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
}

@end
